//
//  ZCTopView.m
//  BaiSi
//
//  Created by 张灿 on 16/5/2.
//  Copyright © 2016年 张灿. All rights reserved.
//

#import "ZCTopView.h"
#import <UIImageView+WebCache.h>
#import "ZCThemeItem.h"
 #import "NSDate+Date.h"

@interface ZCTopView ()

@property (weak, nonatomic) IBOutlet UIImageView *iconView;

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UILabel *timeLabel;

@property (weak, nonatomic) IBOutlet UILabel *text_Label;
@end

@implementation ZCTopView

+ (instancetype)topView
{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}


- (void)setItem:(ZCThemeItem *)item
{
    _item = item;
    //头像
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:item.profile_image]];
    //名字
    self.nameLabel.text = item.name;
    
    //发布时间
    self.timeLabel.text = [self timeString];
    
    //文字内容
    self.text_Label.text = item.text;
}

- (void)awakeFromNib
{
    //只要是从xib加载的都要写上这句话,不然cell会被拉伸
    self.autoresizingMask = UIViewAutoresizingNone;
    
    //用户头像圆形显示
    self.iconView.layer.cornerRadius = self.iconView.width * 0.5;
    self.iconView.layer.masksToBounds = YES;
}

- (IBAction)moreClick:(UIButton *)sender {
    
    //创建alertVC
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *act0 = [UIAlertAction actionWithTitle:@"收藏" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"收藏");
    }];
    [alertVC addAction:act0];
    
    UIAlertAction *act1 = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"举报");
    }];
    [alertVC addAction:act1];
    
    UIAlertAction *act2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"取消");
    }];
    [alertVC addAction:act2];
    
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertVC animated:YES completion:nil];
}

// 返回时间字符串
- (NSString *)timeString
{
    // 获取发布日期对象
    // NSDateFormatter:日期字符串转 日期 ， 日期 转 日期字符串
    NSString *dateStr = self.item.create_time;
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    // 2015-08-24 19:12:20
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *createDate = [fmt dateFromString:dateStr];
    
    NSDateComponents *detalCmp = [createDate detalDateWithNow];
    
    
    if ([createDate isThisYear]) {
        if ([createDate isThisToday]) {
            
            if (detalCmp.hour >= 1) { // 大于1小时
                
                dateStr = [NSString stringWithFormat:@"%ld小时前",detalCmp.hour];
                
            } else if (detalCmp.minute > 1) { // 大于1分钟
                
                dateStr = [NSString stringWithFormat:@"%ld分钟前",detalCmp.minute];
                
            } else { // 刚刚
                dateStr = @"刚刚";
            }
            
        } else if ([createDate isThisYesterday]) { // 发布日期 -> 昨天 19:20:20
            fmt.dateFormat = @"昨天 HH:mm:ss";
            dateStr = [fmt stringFromDate:createDate];
        } else { // 昨天之前 05-01 19:12:20
            
            fmt.dateFormat = @"MM-dd HH:mm:ss";
            dateStr = [fmt stringFromDate:createDate];
        }
        
    }
    
    return dateStr;
}


@end
